const User = require('../../model/manage/User');

module.exports = {
    async login(req, res) {
        let name = req.body.username;
        let password = req.body.pw;
        if(!!name && !!password) {
           let result = await User.login(name, password);

           if(result.code == 200) {
            res.success('登录成功', result.data);
           }else {
               res.error('用户名或密码错误');
           }
        }else {
            res.error('用户名或密码不能为空');
        }
    },
    async register(req, res) {
        let name = req.body.name;
        let password = req.body.pw;
        if(!!name && !!password) {
            let result = await User.register(name, password);

            if(result.code == 200) {
                res.success('注册成功', result.data);
            }else {
                res.error('注册失败');
            }
        }else {
            res.error('用户名和密码不能为空')
        }
    },
    async getInfo(req, res) {
        let id = req.params.id;

        if(id) {
            let result = await User.getInfo(id);

            if(result.code == 200) {
                res.success("获取成功", result.data);
            }else {
                res.error("获取失败");
            }
        }else {
            res.error('用户 id 非法')
        }
    }
}